//
//  AddTokenAddressViewController.m
//  EPBPro
//
//  Created by wucha on 2018/8/1.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "AddTokenAddressViewController.h"
#import "ResizeView.h"
#import "HttpTokenAction.h"

@interface AddTokenAddressViewController (){
    AddTokenBlock addTokenBlock;
}
@property (nonatomic,weak) UITextField *markTF;
@property (nonatomic,weak) UITextField *addressTF;

@end

@implementation AddTokenAddressViewController

-(id)initWithTokenBlock:(AddTokenBlock)block{
    if (self == [super init]) {
        addTokenBlock = block;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleName = @"添加地址";
    self.view.backgroundColor = [UIColor whiteColor];
    ResizeView *resizeView = [[ResizeView alloc] initWithFrame:CGRectMake(0, StatusBarHeight+44, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarHeight-44)];
    [self.view addSubview:resizeView];
    
    [resizeView addResizeSubview:[self getSpaceResizeViewInfo:40]];
    ResizeViewInfo *markRV = [self getNormalResizeViewInfo:@"再次输入地址备注"];
    self.markTF = (UITextField*)[markRV viewWithKey:@"TF"];
    [resizeView addResizeSubview:markRV];
    ResizeViewInfo *addressRV = [self getNormalResizeViewInfo:@"输入你的地址"];
    self.addressTF = (UITextField*)[addressRV viewWithKey:@"TF"];
    [resizeView addResizeSubview:addressRV];
    
    [resizeView addResizeSubview:[self getSpaceResizeViewInfo:30]];
    [resizeView addResizeSubview:[self getSubmitResizeViewInfo]];
    
}

-(ResizeViewInfo*)getSpaceResizeViewInfo:(int)height{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = height;
    return resizeViewInfo;
    
}

-(ResizeViewInfo*)getSubmitResizeViewInfo{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 40;
    
    UIButton *submitButton = [UIButton new];
    submitButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [submitButton setTitle:@"确定" forState:UIControlStateNormal];
    [submitButton addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
    [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    submitButton.backgroundColor = DefaultTenantColor;
    [resizeViewInfo addSubview:submitButton];
    submitButton.sd_layout.leftSpaceToView(resizeViewInfo.view, 40).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).widthIs(SCREEN_WIDTH-80);
    
    return resizeViewInfo;
}

-(void)submitAction:(id)sender{
    if ([self.markTF.text isEqualToString:@""]) {
        [self showMessage:@"备注不能为空" autoDissmissAfterDelay:0];
        return;
    }
    if ([self.addressTF.text isEqualToString:@""]) {
        [self showMessage:@"地址不能为空" autoDissmissAfterDelay:0];
        return;
    }
    AddTokenAddressActionInfo *addTokenAddressActionInfo = [[AddTokenAddressActionInfo alloc] init];
    addTokenAddressActionInfo.addressName = self.markTF.text;
    addTokenAddressActionInfo.address = self.addressTF.text;
    addTokenAddressActionInfo.type = @"key";
    [self showWaitingViewWithMessage:@"提交中..." style:WaitingViewShowStyleFullSelfView];
    [HttpTokenAction postAddTokenAddress:addTokenAddressActionInfo block:^(id result, NSError *error) {
        [self dismissWaitingView];
        if (!error) {
            [self showMessage:@"添加成功" autoDissmissAfterDelay:0];
            if(addTokenBlock){
                addTokenBlock();
            }
            [self performBlock:^{
                [self.navigationController popViewControllerAnimated:YES];
                
            } afterDelay:0.8];
        }else{
            [self showMessage:error.domain autoDissmissAfterDelay:0];

        }
    }];
}

-(ResizeViewInfo*)getNormalResizeViewInfo:(NSString*)valueString{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 45;
    resizeViewInfo.view.backgroundColor = [UIColor whiteColor];
    
    UITextField *textField = [UITextField new];
    textField.placeholder = valueString;
    [resizeViewInfo addSubview:textField viewKey:@"TF"];
    textField.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).topSpaceToView(resizeViewInfo.view, 0).bottomSpaceToView(resizeViewInfo.view, 0).rightSpaceToView(resizeViewInfo.view, 15);
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = DefaultCellLineColor;
    [resizeViewInfo addSubview:lineView];
    lineView.sd_layout.leftSpaceToView(resizeViewInfo.view, 15).bottomSpaceToView(resizeViewInfo.view, 0).rightSpaceToView(resizeViewInfo.view, 15).heightIs(1);
    
    return resizeViewInfo;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
